#include"receiver.h"


using namespace std;






/***************************************
			Class Receiver
***************************************/


Receiver::Receiver()
{
	receiver_running = true;

	server_port = 8888;
	receiver_running = true;
}

Receiver::~Receiver()
{
}

void
Receiver::set_ack()
{
	Clock clock;
	clock.click();
	memset(ack, 0, sizeof(ack));
	strcpy(ack, clock.strTimeClicked);
}

void
Receiver::stop_receiver()
{
	receiver_running = false;
}


void 
Receiver::run_receiver()
{
	preset_receiver();
	start_receiver();
	close(server_fd);
	return ;
}

void 
Receiver::preset_receiver()
{
	server_fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(server_fd<0) 
	{
		ColorPrint::printRed("create socket fail!");	
		return ;
	}

	/*
	struct timeval timeout;
	timeout.tv_sec = 1; //seconds
	timeout.tv_usec = 0; // us
	if( setsockopt( server_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1)
	{
		printf("setsockopt fail!\n");
		return ;		
	}
	*/
	

	memset(&ser_addr, 0, sizeof(ser_addr));
	ser_addr.sin_family=AF_INET;
	ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	ser_addr.sin_port = htons(server_port);

	int ret = bind(server_fd, (struct sockaddr*)&ser_addr, sizeof(ser_addr));
	if(ret<0)
	{
		ColorPrint::printRed("socket bind failed!");	
		return ;
	}
}

void 
Receiver::start_receiver()
{
	char buf[BUFF_LINE_2];
	socklen_t len;

	int count;
	struct sockaddr_in client_addr;

	while(1)
	{
		if(receiver_running == false)
		{
			//break;	
			ColorPrint::printRed("receiver time out!\n");
			return ;
		}

		memset(buf, 0, BUFF_LINE_2);
		len = sizeof(client_addr);
		count = recvfrom(server_fd, buf, BUFF_LINE_2, 0, (struct sockaddr*)&client_addr, &len);
		if(count==-1)
		{
			ColorPrint::printRed("receive data fail!\n");	
			return ;
		}	
		if(strcmp(buf, "END")==0)
		{
			ColorPrint::printYellow("RECEIVER>>\n");
			printf("client end, so end server...\n");
			return ;	
		}
		ColorPrint::printYellow("RECEIVER>>\n");
		set_ack();
		printf("clock:%s\n", ack);
		sendto(server_fd, ack, ACK_LINE, 0, (struct sockaddr*)&client_addr, len);

		printf("client:%s\n", buf);
		memset(buf, 0, BUFF_LINE_2);
		sprintf(buf, "I have received %d bytes data!\n", count);
		ColorPrint::printYellow("RECEIVER>>\n");
		printf("server:%s\n", buf);
		sendto(server_fd, buf, BUFF_LINE_2, 0, (struct sockaddr*)&client_addr, len);
	}
	return ;
}
/***************************************
			Class ReceiverController
***************************************/

Receiver *ReceiverController::udpCs_ptr	= NULL;

void
ReceiverController::udpCs_install( Receiver *udpCs )
{
	udpCs_ptr = udpCs ;
}
void
ReceiverController::udpCs_stop_receiver()
{
	udpCs_ptr->stop_receiver();
}

/***************************************
			Class ReceiverThread
***************************************/
ReceiverThread::ReceiverThread()
{
}

ReceiverThread::~ReceiverThread()
{

	if(t_receiver != NULL)
	{
		delete(t_receiver);	
		t_receiver=NULL;
	}
}

void 
ReceiverThread::run_independent_receiver()
{
	t_receiver = new thread(&Receiver::run_receiver, &udpCs);
}
void 
ReceiverThread::wait_for_receiver()
{
	t_receiver->join();
}

